Use Bootstrap5.x in Django3.x
Due to rapid development of versions, some past instructions are impossible to use. This is the instrcution for newest versions.
Introduction to Bootstrap and Django
Bootstrap is a font framework. It is based on HTML, CSS and Javascript, including reusable modules, jQuery plugin, and grid system. Thus, it is friendly to beginner.
Django is a powerful WEB app framework. It has long history and completed document. The new project is with a auto-generate SQL database.
Steps to insert Bootstrap in Django
Download Django and Bootstrap.
This is the offical download instruction of Django: https://www.djangoproject.com/download/
This is the offical download instruction of Bootstrap:
https://getbootstrap.com/
To check whether they are downloaded and ready to use:
Open Terminal and enter:
1 |
|
If you see ‘x.x.x’, Django has been set correctly.
If you can see a folder called ‘boostrap-x.x.x-dist’, Bootstrap has been set correctly.
Set up a new Django project
Open terminal, enter:
1 |
|
attention: make sure you are in the folder where you want to put your new project in. Otherwise you highlypossible can’t find your project after you build it.
Then you will see a new folder appear.
Explaination about the structure of Django project folder
Often edit:
- myWeb: the project folder
- urls.py: set up the path of html files, like website catalogue
Seldom edit
- asgi.py: An asgi compatible web server portal
- wsgi.py: An wsgi compatible web server portal
- setting.py: Django project settings
- manage.py: support for Django Terminal
This is just the basic files, we may add the following files as we need:
- views.py: to write view functions
- model.py: to write model functions operating SQL
- templates: to store HTML files
- static: to store static files, such as: images, css, js
Explaining about bootstrap files
The css and js stores all inital bootstrap css files and js files. Ensure to import them to every html you write.
Essential modify of files
- In view.py and urls.py:
add the following codes to views.py:add the following code to urls.py:1
2
3
4from django.shortcuts import render
from django.http import HttpRequest,HttpResponse
def home(request):
return render(request,'home.html')Attention: in the newest version of Django, the original ‘url’ are changed to ‘path’1
2
3
4
5
6
7from django.urls import path
from . import views
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', views.home),
] - In the home.html in templates folder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{% load static %}
<head>
// your html head code
<link rel = "stylesheet" href = "{% static 'bootstrap/css/bootstrap.min.css'%}">
<link rel="stylesheet" href="{% static 'css/style.css'%}" />
<link rel = "stylesheet" href="{% static 'css/home.css'%}"/>
</head>
<body>
// your html body code
<script src = "{% static 'bootstrap/js/bootstrap.min.js' %}"></script>
</body> - In static folder
- create new folder called ‘bootstrap’
- In the bootstrap folder, create new folders: css, js
- Copy the css and js files in the downloaded bootstrap folders to static correspondence folder.
- In the settings.py
Modify following codes, if they are not exist, add them:You can check whether the path is correct by enter following codes in Terminal:1
2
3
4
5STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, "root")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "statics"),
]
1 |
|
Then all the static files in the path will appear in root.
Run the test server
1 |
|
You can see the page!
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!